package Delegate;

import gui.ChatListItem;

import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.core.QRect;
import com.trolltech.qt.core.QRectF;
import com.trolltech.qt.core.QSize;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.core.Qt.AlignmentFlag;
import com.trolltech.qt.gui.QBrush;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QItemDelegate;
import com.trolltech.qt.gui.QLinearGradient;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QStyle;
import com.trolltech.qt.gui.QStyleOptionViewItem;
import com.trolltech.qt.gui.QColor;

public class ChatItemDelegate extends QItemDelegate {
	
	public void paint(QPainter painter, QStyleOptionViewItem option, QModelIndex index){
		Object data = index.data();
		if (data != null && data instanceof ChatListItem) {
        	ChatListItem item=(ChatListItem)data;
        	QRect rect = option.rect();
    		int left=rect.left();
    		int right=rect.right();
    		int top=rect.top();
    		int bottom=rect.bottom();
    		QLinearGradient gradient;
    		
        	
    		rect.setRight(right-50);
			rect.setBottom(rect.top()+15);
			gradient=new QLinearGradient(rect.left(),rect.top(),rect.left(),rect.bottom()-1);
			gradient.setColorAt(0, new QColor(231,239,253));
	    	gradient.setColorAt(1, new QColor(203,218,241));
	        painter.setBrush(gradient);
	        painter.setPen(new QColor(139,150,222));
	        rect.setBottom(rect.bottom()-1);
	        painter.drawRoundRect(rect, 3);
    		
	        rect.setTop(rect.bottom());
			rect.setBottom(bottom);
			gradient=new QLinearGradient(rect.left(),rect.top()+1,rect.left(),rect.bottom());
			gradient.setColorAt(0, new QColor(245,243,197));
	    	gradient.setColorAt(1, new QColor(249,224,172));
	        painter.setBrush(gradient);
	        painter.setPen(new QColor(236,179,115));
	        rect.setTop(rect.top()+1);
	        painter.drawRoundRect(rect, 3);
    		
	        
            rect.setTop(top);
        	gradient=new QLinearGradient(option.rect().left(),option.rect().top(),option.rect().left(),option.rect().bottom());
        	if(option.state().isSet(QStyle.StateFlag.State_Selected)){
        		gradient.setColorAt(0, new QColor(110,161,241));
            	gradient.setColorAt(1, new QColor(86,125,188));
            	painter.setBrush(gradient);
	            painter.setPen(new QColor(191,205,228));
	            painter.drawRoundRect(rect, 3);
//	            painter.fillRect(option.rect(), option.palette().highlight());
        	}
        	else if (option.state().isSet(QStyle.StateFlag.State_MouseOver)) {
        		gradient.setColorAt(0, new QColor(231,239,253));
            	gradient.setColorAt(1, new QColor(203,218,241));
            	painter.setBrush(gradient);
	            painter.setPen(new QColor(191,205,228));
	            painter.drawRoundRect(rect, 3);
            }
        	
        	
    		rect.setLeft(right-50);
    		rect.setRight(right);
    		this.drawDecoration(painter, option, rect, item.getIcon().pixmap(50, 50));
    		rect.setLeft(left);
            rect.setRight(right-50);
            
            item.getDoc().drawContents(painter,new QRectF(rect));
        }
		
		else{
			QIcon icon = (QIcon)index.data(Qt.ItemDataRole.DecorationRole);
			String text=(String)index.data(Qt.ItemDataRole.DisplayRole);
			
			QRect rect = option.rect();
			QRect rect0 = rect.clone();
			int width= rect.right();
			int height=rect.height();
			rect.setRight(rect.left() + 50);
			this.drawDecoration(painter, option, rect, icon.pixmap(50, 50));
			
			rect.setRight(width);
			rect.setLeft(rect.left() + 50);
			rect.setBottom(rect.top()+15);
			QRect rect1=rect.clone();
			QLinearGradient gradient=new QLinearGradient(rect.left(),rect.top(),rect.left(),rect.bottom()-1);
			gradient.setColorAt(0, new QColor(231,239,253));
	    	gradient.setColorAt(1, new QColor(203,218,241));
	        painter.setBrush(gradient);
	        painter.setPen(new QColor(139,150,222));
	        rect.setBottom(rect.bottom()-1);
	        painter.drawRoundRect(rect, 3);
	        
	        
			rect.setTop(rect.bottom());
			rect.setBottom(rect.top()+height-15);
			QRect rect2=rect.clone();
			gradient=new QLinearGradient(rect.left(),rect.top()+1,rect.left(),rect.bottom());
			gradient.setColorAt(0, new QColor(245,243,197));
	    	gradient.setColorAt(1, new QColor(249,224,172));
	        painter.setBrush(gradient);
	        painter.setPen(new QColor(236,179,115));
	        rect.setTop(rect.top()+1);
	        painter.drawRoundRect(rect, 3);
	        
	        
	        
	        if(option.state().isSet(QStyle.StateFlag.State_Selected)){
	        	rect0.setLeft(rect0.left()+50);
	        	gradient=new QLinearGradient(rect0.left(),rect0.top(),rect0.left(),rect0.bottom());
	    		gradient.setColorAt(0, new QColor(110,161,241));
	        	gradient.setColorAt(1, new QColor(86,125,188));
	        	painter.setBrush(gradient);
	            painter.setPen(new QColor(191,205,228));
	            painter.drawRoundRect(rect0, 3);
	    	}
	        else if (option.state().isSet(QStyle.StateFlag.State_MouseOver)) {
	        	rect0.setLeft(rect0.left()+50);
        		gradient.setColorAt(0, new QColor(231,239,253));
            	gradient.setColorAt(1, new QColor(203,218,241));
                painter.setBrush(gradient);
	            painter.setPen(new QColor(191,205,228));
	            painter.drawRoundRect(rect0, 3);
            }
	        
	        
	        rect1.setLeft(rect1.left()+3);
	        rect2.setLeft(rect2.left()+3);
	        painter.setPen(QColor.blue);
	        if(text.split("#")[1].equalsIgnoreCase("me"))
	        	painter.drawText(rect1, AlignmentFlag.AlignVCenter.value(), "Me :");
	        else
	        	painter.drawText(rect1, AlignmentFlag.AlignVCenter.value(), text.split("#")[2]+":");
			painter.setPen(QColor.black);
			painter.drawText(rect2, AlignmentFlag.AlignVCenter.value(), text.split("#")[0]);
			
		}
	}
	
	public QSize sizeHint(QStyleOptionViewItem option, QModelIndex index)
    {
        Object data = index.data();
        
        if (data instanceof ChatListItem){
        	QSize size=((ChatListItem) data).getDoc().size().toSize();
        	if(size.height()<50) size.setHeight(50);
        	return size;
        }
        else{
        	QSize size=super.sizeHint(option, index);
        	String ss[]=((String)data).split("#");
        	String enters[]=ss[0].split("\n");
        	int countLine=enters.length;
        	int needHeight=countLine*15+16;
        	if(needHeight>50)
        		size.setHeight(needHeight);
        	return size;
        }
    }
}
